package com.pow.repository;

import java.util.List;

import javax.transaction.Transactional;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import com.pow.entity.Role;
import com.pow.entity.User;

public interface UserRepository extends JpaRepository<User, String>,JpaSpecificationExecutor<User>{

	/**
	 * 通过uid,rid来查当前用户是否拥有该角色
	 * @param uid
	 * @param rid
	 * @return 0没有 1有
	 */
	@Query(value="select count(*) FROM user_role_tb  where user_role_tb_user_id=?1 and user_role_tb_role_id=?2",nativeQuery=true)
	Integer  getUser_Role(String uid,String rid);
	/**
	 * 添加user和role
	 * @param uid
	 * @param rid
	 * @return 返回是否成功
	 */
	@Query(value="INSERT INTO `sister`.`user_role_tb` (`user_role_tb_user_id`, `user_role_tb_role_id`) VALUES (?1,?2)",nativeQuery=true)
	@Modifying
	@Transactional
	Integer addUser_Role(String uid,String rid);
	
	/**
	 * 删除之前选中但修改后未选择的信息
	 * @param uid
	 * @param rid
	 * @return 0或 1或大于1
	 */
	@Query(value="delete  from user_role_tb  where user_role_tb_user_id=?1 and user_role_tb_role_id =?2",nativeQuery=true)
	@Modifying
	@Transactional
	Integer deleteUser_Role(String uid ,String rid);
	
	/**
	 * 查询当前用户拥有的角色
	 * @param uid
	 * @return
	 */
	@Query(value="select ur.user_role_tb_role_id FROM user_role_tb ur where ur.user_role_tb_user_id=?1",nativeQuery=true)
	List<String> getRidsByUid(String uid);
}
